/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.schedule.core.service.processor.edit;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.schedule.core.domain.ScheduleJob;
import info.peigen.hotpot.component.schedule.core.service.processor.AbstractScheduleServiceProcessor;
import info.peigen.hotpot.component.schedule.facade.command.order.ScheduleJobEditOrder;
import org.noear.solon.annotation.Component;

/**
 * <b>(ScheduleJobEditProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/7
 */
@Component
public class ScheduleJobEditProcessor extends AbstractScheduleServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 20;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        ScheduleJobEditOrder editOrder   = (ScheduleJobEditOrder) serviceContext.getOrder();
        ScheduleJob          scheduleJob = (ScheduleJob) serviceContext.getDomain();
        if (StrUtil.isNotBlank(editOrder.getJobName())) {
            scheduleJob.setJobName(editOrder.getJobName());
        }

        if (StrUtil.isNotBlank(editOrder.getCron())) {
            scheduleJob.setCron(editOrder.getCron());
        }

        if (ObjUtil.isNotNull(editOrder.getEnable())) {
            scheduleJob.setYesOrNo(editOrder.getEnable());
        }

        if (ObjUtil.isNotNull(editOrder.getCommunicationMethod())) {
            scheduleJob.setCommunicationMethod(editOrder.getCommunicationMethod());
        }

        if (StrUtil.isNotBlank(editOrder.getUrl())) {
            scheduleJob.setUrl(editOrder.getUrl());
        }

        if (StrUtil.isNotBlank(editOrder.getEndPoint())) {
            scheduleJob.setEndPoint(editOrder.getEndPoint());
        }

        if (ObjUtil.isNotNull(editOrder.getPreExecuteTime())) {
            scheduleJob.setPreExecuteTime(editOrder.getPreExecuteTime());
        }

        if (ObjUtil.isNotNull(editOrder.getPreExecuteResultCode())) {
            scheduleJob.setPreExecuteResultCode(editOrder.getPreExecuteResultCode());
        }

        if (ObjUtil.isNotNull(editOrder.getNextExecuteTime())) {
            scheduleJob.setNextExecuteTime(editOrder.getNextExecuteTime());
        }

        if (editOrder.getExecuteTimeout() >= 0) {
            scheduleJob.setExecuteTimeout(editOrder.getExecuteTimeout());
        }

        if (editOrder.getExecuteFailRetryCount() >= 0) {
            scheduleJob.setExecuteFailRetryCount(editOrder.getExecuteFailRetryCount());
        }

        if (StrUtil.isNotBlank(editOrder.getMemo())) {
            scheduleJob.setMemo(editOrder.getMemo());
        }
    }
}